package gui.dialog;

import gui.TuristAssistant;

import com.cloudgarden.resource.SWTResourceManager;

import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.MenuDetectEvent;
import org.eclipse.swt.events.MenuDetectListener;

import org.example.www.webBlogService.Comment;

/**
 * Representa a un {@link Comment} en la interfaz.
 * 
 * @author Gonzalo Maso Talou
 *
 */
public class CommentItemComposite extends org.eclipse.swt.widgets.Composite {

	{
		// Register as a resource user - SWTResourceManager will
		// handle the obtaining and disposing of resources
		SWTResourceManager.registerResourceUser(this);
	}

	private Composite compositeAvatar;
	private StyledText styledText;
	/**
	 * {@link Comment} a que representa el Item.
	 */
	private Comment comment;

	public CommentItemComposite(org.eclipse.swt.widgets.Composite parent,
			int style, Comment comment) {
		super(parent, style);
		this.comment = comment;
		initGUI();
	}

	private void initGUI() {
		try {
			GridLayout thisLayout = new GridLayout();
			thisLayout.numColumns = 2;
			this.setLayout(thisLayout);
			this.setSize(590, 90);
			{
				compositeAvatar = new Composite(this, SWT.BORDER);
				GridLayout compositeAvatarLayout = new GridLayout();
				compositeAvatarLayout.makeColumnsEqualWidth = true;
				GridData compositeAvatarLData = new GridData();
				compositeAvatarLData.widthHint = 80;
				compositeAvatarLData.heightHint = 80;
				compositeAvatar.setLayoutData(compositeAvatarLData);
				compositeAvatar.setLayout(compositeAvatarLayout);
				compositeAvatar.setBackgroundImage(new Image(Display
						.getCurrent(), (TuristAssistant.images.get(comment
						.getUser().getAvatar())).getImageData().scaledTo(80,
						80)));
			}
			{
				GridData styledTextLData = new GridData();
				styledTextLData.horizontalAlignment = GridData.FILL;
				styledTextLData.grabExcessHorizontalSpace = true;
				styledTextLData.verticalAlignment = GridData.FILL;
				styledText = new StyledText(this, SWT.NONE);
				styledText.setLayoutData(styledTextLData);
				styledText.setBackground(SWTResourceManager.getColor(240, 240,
						240));
				styledText.addMenuDetectListener(new MenuDetectListener() {
					public void menuDetected(MenuDetectEvent evt) {
						styledTextMenuDetected(evt);
					}
				});
				styledText.setText(comment.getUser().getUserName()+" says:\n"+comment.getText());
			}
			this.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void styledTextMenuDetected(MenuDetectEvent evt) {
		setBackground(SWTResourceManager.getColor(150, 150, 255));
		styledText.setBackground(SWTResourceManager.getColor(150, 150, 255));
		CommSendEMailDialog dialog = new CommSendEMailDialog(this.getShell(),this.getStyle());
		dialog.setEMailRecipient(comment.getUser().getUserName());
		dialog.open();
		setBackground(SWTResourceManager.getColor(240, 240, 240));
		styledText.setBackground(SWTResourceManager.getColor(240, 240, 240));
	}

}
